package com.morgan.maxfunding;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.util.Log;
import android.view.View;

import com.morgan.maxfunding.MainActivity;
import com.morgan.submit.Asset;
import com.morgan.submit.BusinessDetail;
import com.morgan.submit.CreditHistory;
import com.morgan.submit.EmploymentCompanyDetail;
import com.morgan.submit.Liabilities;
import com.morgan.submit.MotorVehicle1;
import com.morgan.submit.MotorVehicle2;
import com.morgan.submit.RealEstate1;
import com.morgan.submit.RealEstate2;

public class SubmitActivity extends Activity implements View.OnClickListener {
	
	LinearLayout llytHeader;

	final static int VIEW_ASSET 			= 0;
	final static int VIEW_MOTOR_VEHICLE 	= 1;
	final static int VIEW_REAL_ESTATE 		= 2;

	LinearLayout 	llytAsset;
	
	LinearLayout 	llytMotorVehicle1;
	LinearLayout 	llytMotorVehicle2;
	
	LinearLayout 	llytRealEstate1;
	LinearLayout 	llytRealEstate2;
	
	public Button 			btnAsset;
	public Button 			btnMotorVehicle;
	public Button			btnRealEstate;
	
	Button			btnBack;
	Button			btnNext;
	
	Asset 			viewAsset;
	MotorVehicle1 	viewMotorVehicle1;
	MotorVehicle2 	viewMotorVehicle2;
	RealEstate1		viewRealEstate1;
	RealEstate2		viewRealEstate2;
	
//	Button 			btnNeedHelp;
//	Button 			btnGoSite;

	
	int				m_nSelectView;
	int 			nErrorCode;
	
	ScrollView		scrollView;
//    public static ProgressDialog dlgLoading;

    boolean			m_isHasPersonalLoan;
    @Override
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_submit);
        
        GlobalData.makeItemsOfRegisteredOwnersNameForMortorVehicle();
        GlobalData.makeItemsOfRegisteredOwnersNameForRealEstate();
        
        initWidget();
        initEvent();
        initValue();
    }
    
    private void initValue() {
    	m_isHasPersonalLoan = true;
    	m_nSelectView = VIEW_ASSET;
    	selectView(m_nSelectView);
    }
    
    private void savePreferences() {
    	viewAsset.savePreferences();
    	viewMotorVehicle1.savePreferences();
    	viewMotorVehicle2.savePreferences();
    	viewRealEstate1.savePreferences();
    	viewRealEstate2.savePreferences();
    }
    
    private void selectView(int nView) {
    	
    	llytAsset.setVisibility(View.GONE);
    	llytMotorVehicle1.setVisibility(View.GONE);
    	llytMotorVehicle2.setVisibility(View.GONE);
    	llytRealEstate1.setVisibility(View.GONE);
    	llytRealEstate2.setVisibility(View.GONE);
    	
    	savePreferences();
    	
    	m_nSelectView = nView;
    	if (m_nSelectView == VIEW_ASSET) {
    		btnAsset.requestFocusFromTouch();
    		llytAsset.setVisibility(View.VISIBLE);
    	}
    	if (m_nSelectView == VIEW_MOTOR_VEHICLE) {
    		btnMotorVehicle.requestFocusFromTouch();

    		if (GlobalData.personalInfo.countMotorVehicle > 0) {
    			llytMotorVehicle1.setVisibility(View.VISIBLE);
    		}
    		if (GlobalData.personalInfo.countMotorVehicle > 1) {
    			llytMotorVehicle2.setVisibility(View.VISIBLE);
    		}
    	}
    	if (m_nSelectView == VIEW_REAL_ESTATE) {
    		btnRealEstate.requestFocusFromTouch();
    		if (GlobalData.personalInfo.countRealState > 0) {
    			llytRealEstate1.setVisibility(View.VISIBLE);
    		}
    		if (GlobalData.personalInfo.countRealState > 1) {
    			llytRealEstate2.setVisibility(View.VISIBLE);	
    		}
    	}

    }
    
    private void initWidget() {
    	
		llytHeader			= (LinearLayout) findViewById(R.id.header_linearLayout);

    	scrollView			= (ScrollView) findViewById(R.id.scrollView1);
    	
    	llytAsset 			= (LinearLayout) findViewById(R.id.asset_layout);
    	
    	llytMotorVehicle1 	= (LinearLayout) findViewById(R.id.motor_vehicle_layout1);
    	llytMotorVehicle2 	= (LinearLayout) findViewById(R.id.motor_vehicle_layout2);
    	
    	llytRealEstate1 	= (LinearLayout) findViewById(R.id.real_estate_layout1);
    	llytRealEstate2 	= (LinearLayout) findViewById(R.id.real_estate_layout2);
    	
    	btnAsset 			= (Button) findViewById(R.id.no_credit_history_button);
    	btnMotorVehicle 	= (Button) findViewById(R.id.yes_credit_history_button);
    	btnRealEstate		= (Button) findViewById(R.id.real_estate_button);

    	btnBack				= (Button) findViewById(R.id.back_button);
    	btnNext				= (Button) findViewById(R.id.next_submit_button);
//    	btnNeedHelp			= (Button) findViewById(R.id.need_help_button);
//    	btnGoSite 			= (Button) findViewById(R.id.go_site_button);

		viewAsset 			= new Asset(this);
		
		viewMotorVehicle1 	= new MotorVehicle1(this);
		viewMotorVehicle2 	= new MotorVehicle2(this);
		
		viewRealEstate1 	= new RealEstate1(this);
		viewRealEstate2 	= new RealEstate2(this);
				
		llytAsset.addView(viewAsset.view);
		
		llytMotorVehicle1.addView(viewMotorVehicle1.view);
		llytMotorVehicle2.addView(viewMotorVehicle2.view);
		
		llytRealEstate1.addView(viewRealEstate1.view);
		llytRealEstate2.addView(viewRealEstate2.view);
	}
    
    private void initEvent() {
    	
		llytHeader.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://maxfunding.com.au"));
				startActivity(browserIntent);
			}
        });
		
//    	btnNeedHelp.setOnClickListener(new Button.OnClickListener() {
//    		
//			@Override
//			public void onClick(View v) {
//				final AlertDialog.Builder alert = new AlertDialog.Builder(SubmitActivity.this);
//			    final EditText input = new EditText(SubmitActivity.this);
//			    alert.setTitle("What issues are you experiencing?");
//			    alert.setView(input);
//			    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
//			        public void onClick(DialogInterface dialog, int whichButton) {
////			            dialog.cancel();
//			        }
//			    });
//			    alert.setPositiveButton("Talk to Us", new DialogInterface.OnClickListener() {
//			        public void onClick(DialogInterface dialog, int whichButton) {
//
//			        	Intent email = new Intent(Intent.ACTION_SEND_MULTIPLE);
//			        	email.putExtra(Intent.EXTRA_EMAIL, new String[] {"victor@morganfinance.com.au"});
//			            email.putExtra(Intent.EXTRA_SUBJECT, "Mobile App technical error report");
//			            email.putExtra(Intent.EXTRA_TEXT, input.getText().toString());
//			        	email.setType("text/plain");			             
//
//			            startActivity(Intent.createChooser(email, "E-mail"));
//			        }
//			    });
//			    
//			    alert.show();
//			}
//        });
    	
//    	btnGoSite.setOnClickListener(new Button.OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.morganfinance.com.au/mobile_index.php"));
//				startActivity(browserIntent);
//			}
//        });
    	
    	btnBack.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
        });
    	
    	btnNext.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				
				savePreferences();
				if (!viewAsset.checkInput()) return;
				if (!checkMotorVehicle()) return;
				
				SubmitActivity.this.startActivity(new Intent(SubmitActivity.this, NewSubmitActivity.class));
			}
        });
    	
    	btnAsset.setOnClickListener(this);
    	btnMotorVehicle.setOnClickListener(this);
    	btnRealEstate.setOnClickListener(this);
    }
        
    public boolean checkMotorVehicle() {
    	if (GlobalData.personalInfo.countMotorVehicle > 0) {
			
			nErrorCode = viewMotorVehicle1.checkInput();
			if (nErrorCode != MotorVehicle1.ERROR_NONE) {
				selectView(VIEW_MOTOR_VEHICLE);
				
				if (nErrorCode == MotorVehicle1.ERROR_CURRENT_VALUE1)
					viewMotorVehicle1.edtCurrentValue1.requestFocus();
				if (nErrorCode == MotorVehicle1.ERROR_BRAND1)
					viewMotorVehicle1.edtBrand1.requestFocus();
				if (nErrorCode == MotorVehicle1.ERROR_MODEL1)
					viewMotorVehicle1.edtModel1.requestFocus();
				if (nErrorCode == MotorVehicle1.ERROR_REGISTERED_OWNER_NAME) {					
					viewMotorVehicle1.spnRegisteredOwnersName1.requestFocusFromTouch();
				}
				if (nErrorCode == MotorVehicle1.ERROR_UNDER_FINANCE) {
					viewMotorVehicle1.btnNoUnderFinance1.requestFocusFromTouch();
					viewMotorVehicle1.btnYesUnderFinance1.requestFocusFromTouch();
				}
				if (nErrorCode == MotorVehicle1.ERROR_YEAR) {					
					viewMotorVehicle1.spnYear1.requestFocusFromTouch();
				}
				if (nErrorCode == MotorVehicle1.ERROR_INSURANCE) {					
					viewMotorVehicle1.spnInsurance1.requestFocusFromTouch();
				}
				
				return false;
			}
		}
		
		if (GlobalData.personalInfo.countMotorVehicle > 1) {
			
			nErrorCode = viewMotorVehicle2.checkInput();
			if (nErrorCode != MotorVehicle2.ERROR_NONE) {
				selectView(VIEW_MOTOR_VEHICLE);
				
				if (nErrorCode == MotorVehicle2.ERROR_CURRENT_VALUE2)
					viewMotorVehicle2.edtCurrentValue2.requestFocus();
				if (nErrorCode == MotorVehicle2.ERROR_BRAND2)
					viewMotorVehicle2.edtBrand2.requestFocus();
				if (nErrorCode == MotorVehicle2.ERROR_MODEL2)
					viewMotorVehicle2.edtModel2.requestFocus();
				if (nErrorCode == MotorVehicle2.ERROR_REGISTERED_OWNER_NAME) {					
					viewMotorVehicle2.spnRegisteredOwnersName2.requestFocusFromTouch();
				}
				if (nErrorCode == MotorVehicle1.ERROR_UNDER_FINANCE) {
					viewMotorVehicle2.btnNoUnderFinance2.requestFocusFromTouch();
					viewMotorVehicle2.btnYesUnderFinance2.requestFocusFromTouch();
				}
				if (nErrorCode == MotorVehicle2.ERROR_YEAR) {
					viewMotorVehicle2.spnYear2.requestFocusFromTouch();
				}
				if (nErrorCode == MotorVehicle2.ERROR_INSURANCE) {	
					viewMotorVehicle2.spnInsurance2.requestFocusFromTouch();
				}
				
				return false;
			}
		}
		
		return checkRealEstate();
    }
    
    public void showGeneralAlert(String title, String message) {
    	new AlertDialog.Builder(SubmitActivity.this)
        .setTitle(title)
        .setMessage(message)
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // continue with delete
            }
         })
         .show();
    }
    
    public boolean checkRealEstate() {
    	if (GlobalData.personalInfo.countRealState > 0) {
			
			nErrorCode = viewRealEstate1.checkInput();
			
			if (nErrorCode != RealEstate1.ERROR_NONE) {
				selectView(VIEW_REAL_ESTATE);
				
				if (nErrorCode == RealEstate1.ERROR_MARKET_VALUE1) {
					viewRealEstate1.edtMarketValue1.requestFocus();
				}
				if (nErrorCode == RealEstate1.ERROR_ADDRESS1) {
					viewRealEstate1.edtAddress1.requestFocus();
				}
				if (nErrorCode == RealEstate1.ERROR_MORTGAGAE_ARREAR) {
					viewRealEstate1.btnNoAnyMortgageArrear1.requestFocusFromTouch();
					viewRealEstate1.btnYesAnyMortgageArrear1.requestFocusFromTouch();
				}
				if (nErrorCode == RealEstate1.ERROR_REGISTER_OWNER_NAME) {
					viewRealEstate1.spnRegisteredOwnersName1.requestFocusFromTouch();
				}
				if (nErrorCode == RealEstate1.ERROR_ESTATE_TYPE) {
					viewRealEstate1.spnEstateType1.requestFocusFromTouch();
				}
				if (nErrorCode == RealEstate1.ERROR_ESTATE_DESCRIPTION) {
					viewRealEstate1.spnEstateDescription1.requestFocusFromTouch();
				}
				
				return false;
			}
		}
			
		if (GlobalData.personalInfo.countRealState > 1) { 
			nErrorCode = viewRealEstate2.checkInput();
			if (nErrorCode != RealEstate2.ERROR_NONE) {
				selectView(VIEW_REAL_ESTATE);
				
				if (nErrorCode == RealEstate2.ERROR_MARKET_VALUE2) {
					viewRealEstate2.edtMarketValue2.requestFocus();
				}
				if (nErrorCode == RealEstate2.ERROR_ADDRESS2) {
					viewRealEstate2.edtAddress2.requestFocus();
				}
				if (nErrorCode == RealEstate2.ERROR_MORTGAGAE_ARREAR) {
					viewRealEstate2.btnNoAnyMortgageArrear2.requestFocusFromTouch();
					viewRealEstate2.btnYesAnyMortgageArrear2.requestFocusFromTouch();
				}
				if (nErrorCode == RealEstate2.ERROR_REGISTER_OWNER_NAME) {
					viewRealEstate2.spnRegisteredOwnersName2.requestFocusFromTouch();
				}
				if (nErrorCode == RealEstate2.ERROR_ESTATE_TYPE) {
					viewRealEstate2.spnEstateType2.requestFocusFromTouch();
				}
				if (nErrorCode == RealEstate2.ERROR_ESTATE_DESCRIPTION) {
					viewRealEstate2.spnEstateDescription2.requestFocusFromTouch();
				}
				return false;
			}
		}
		
		return true;
    }
    
	public void onClick(View v) {
		if (v == btnAsset) {
			selectView(VIEW_ASSET);
		}
		if (v == btnMotorVehicle) {
			selectView(VIEW_MOTOR_VEHICLE);
		}
		if (v == btnRealEstate) {
			selectView(VIEW_REAL_ESTATE);
		}
	}
	
    public void onBackPressed(){
        // do something here and don't write super.onBackPressed()
    	super.onBackPressed();
    	savePreferences();
   }
}
